package com.gitee.wsl.collections.queue

import com.gitee.wsl.func.consumer.Consumer


class BatchQueue<T>(private val batchSize: Int,
                    private val onFlush: Consumer<List<T>>) {
    private val items = ArrayList<T>()

    fun add(item: T) {
        items.add(item)
        if (items.size >= batchSize) {
            onFlush.accept(items)
            items.clear()
        }
    }

    fun addAll(items: Collection<T>) {
            for (item in items) {
                add(item)
            }
    }

    fun flush() {
        if (!items.isEmpty()) {
            onFlush.accept(items)
            items.clear()
        }
    }
}